function yerefresh(){
    location.reload(true);
}
function hide(){
    if($("#searchheader").css("display")=="none"){
        $("#searchheader").css("display","block");
        $("#hide").text("收起筛选")
    }else if($("#searchheader").css("display")=="block"){
        $("#searchheader").css("display","none");
        $("#hide").text("打开筛选")
    }
}
function aa(res1) {
    if(res1==0){
        return '未审核';
    }else if(res1==1){
        return '已通过';
    }else if(res1==2){
        return '未通过';
    }
}
window.name="upload";
function refresh1() {
    tableIn.reload("demo");
}
var tableIn=null;
var treenode11=null;
var title1=null;
var data1=null;
$.ajax({
    type:'post',
    url: '/advanced/selectallheader',
    success:function(res){
        console.log(res.selectallcourse);
        console.log(res.selectdsh);
        console.log(res.selectwsj);
        console.log(res.selectwtg);
        console.log(res.selectysj);
        $("#selectallcourse").text(res.selectallcourse);
        $("#selectdsh").text(res.selectdsh);
        $("#selectwsj").text(res.selectwsj);
        $("#selectwtg").text(res.selectwtg);
        $("#selectysj").text(res.selectysj);
        $("#qbkc").css("background-color","pink");
    }
})
myajax = $.ajax({
    type:'post',
    url: '/advanced/getxialatree',
    success:function(res){
        data1=res.data;
        console.log(data1)
    }
})
$.when(myajax).done(function () {
    layui.use(['table','dropdown','form'], function () {
        console.log(data1)
        var form=layui.form
        var dropdown = layui.dropdown
        var table = layui.table;
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            var tr = obj.tr;
            if(layEvent === 'detail'){ //查看
                localStorage.setItem("selectmicroid",data.id)
                layer.open({
                    type: 2,
                    title: '查看微图',
                    content: "../micro/selectmicro.html",
                    area: ['80%', '80%'],
                })
            }else if(layEvent === 'audit'){


                layer.open({
                    type: 2,
                    title: '审核课程',
                    content: "microauditprocess.html",
                    area: ['80%', '80%'],
                    success: function (layero, index) {//弹出层成功弹出后的回调函数
                        // 父页面向子页面传参 -- iframe 层传数据
                        var body = layer.getChildFrame('body', index);//获取iframe页面
                        body.find('#aid').val(obj.data.id);
                        body.find('#name').val(obj.data.name);
                    }
                })
            }
        });
        //第一个实例
        tableIn = table.render({
            elem: '#demo'
            , height: 800
            , id: 'demo'
            , url: '/advanced/getmicrolist' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: '序号',},
                {field: 'piano', title: '微圈图片',templet:function(res){
                        return '<div><img src="'+res.piano+'" width="70px" height="70px"></a></div>';
                    }},
                {field: 'articletitle', title: '文章名称'},
                {field: 'classify', title: '所属分类'},
                {field: 'clicks', title: '点击量'},
                {field: 'appstatus',width:100, title: '状态',
                    templet: function (d) {return aa(d.appstatus)}},
                {title: '操作', toolbar: '#barDemo'}
            ]],
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.state, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.total, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
        });
        $("#selectkc").click(function(){
            var name=$("#name").val();
            var articletitle=$("#articletitle").val();
            var useraccount=$("#useraccount").val();
            console.log(name);
            table.reload('demo',{
                where:{
                    name:name,
                    articletitle:articletitle,
                    useraccount:useraccount,
                }
            })
        })
    });
    $(document).ready(function () {
        $.ajax({
            type: "get",
            url: "/intermediate/gettree",
            async: true,
            success: function (res) {
                console.log(res.menulists)
                zTreeObj = $.fn.zTree.init($("#treeDemo"), {
                    async: {
                        enable: true
                    },
                    edit: {
                        // enable: true,
                        drag: {
                            isMove: true, // 打开移动功能，鼠标左键点击后拖拽
                            prev: true,
                            autoOpenTime: 0
                        },
                        editNameSelectAll: true,
                        showRemoveBtn: true,
                        removeTitle: "删除节点",
                        renameTitle: "编辑节点名称",
                        showRenameBtn: true,
                    },
                    view: {
                        selectedMulti: false,
                        showIcon: true,// 显示图标
                        showLine: true,
                        // addHoverDom: addHoverDom
                        // removeHoverDom : removeHoverDom
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: 'id',
                            pIdKey: 'pid',
                            rootPId: 0
                        },
                        key: {
                            name: "name"
                        }
                    },
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        chkboxType: {"Y": "s", "N": "s"}
                    },
                    callback: {
                        onRightClick: onRightClick

                    }
                }, res.menulists); //初始化树
                zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
            }
        });
    });
});
function refresh(){
    $.ajax({
        type: "get",
        url: "/intermediate/gettree",
        async: true,
        success: function (res) {
            a =  res.menulists;
            zTreeObj = $.fn.zTree.init($("#treeDemo"), {
                async: {
                    enable: true
                },
                edit: {
                    // enable: true,
                    drag: {
                        isMove: true, // 打开移动功能，鼠标左键点击后拖拽
                        prev: true,
                        autoOpenTime: 0
                    },
                    editNameSelectAll: true,
                    showRemoveBtn: true,
                    removeTitle: "删除节点",
                    renameTitle: "编辑节点名称",
                    showRenameBtn: true,
                },
                view: {
                    selectedMulti: false,
                    showIcon: true,// 显示图标
                    showLine: true,
                    // addHoverDom: addHoverDom
                    // removeHoverDom : removeHoverDom
                },
                data: {
                    simpleData: {
                        enable: true,
                        idKey: 'id',
                        pIdKey: 'pid',
                        rootPId: 0
                    },
                    key: {
                        name: "name"
                    }
                },
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    chkboxType: {"Y": "s", "N": "s"}
                },
                callback: {
                    onRightClick: onRightClick
                }
            }, a); //初始化树
            zTreeObj.expandAll(true);   //true 节点全部展开、false节点收缩
        }
    });
}
function del(){
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    hideRMenu();
    var id1=$("#ak").val();
    console.log(1);
    if (confirm("是否删除？")) {  // 询问是否删除，若删除，则利用Ajax 技术异步删除，同是返回json格式数据，告知前台是否删除成功！
        $.ajax({
            url: "/primary/del",
            dataType: 'json',
            type: 'post',
            data: {"id": id1},
            success: function (response) {
                console.log(treenode11);
                zTree.removeNode(treenode11);     // z-Tree 的api ，从视角上 删除
                alert(response.msg);
            },
            error: function (response) {
                alert(response.msg);
                return false;
            }
        });
    }
    return;
}
function edit(){
    var pid3=$("#fileNode_pid").val();
    var title1=$("#fileNode_pname").val();
    var id1=$("#ak").val();
    layer.open({
        type: 2,
        title: '修改节点',
        content: "treeedit.html",
        area: ['80%', '80%'],
        success:function(layero,index){
            $("#rMenu").css({ "visibility": "hidden"});
            let childFrame=layer.getChildFrame('body',index);
            childFrame.find('input[name="pid"]').val(pid3);
            childFrame.find('input[name="name"]').val(title1);
            childFrame.find('input[name="id"]').val(id1);

        }
    })
}
function addNode(){
    var id2=$("#ak").val();
    layer.open({
        type: 2,
        title: '添加节点',
        content: "treeadd.html",
        area: ['80%', '80%'],
        success:function(layero,index){
            $("#rMenu").css({ "visibility": "hidden"});
            let childFrame=layer.getChildFrame('body',index);
            childFrame.find('input[name="pid"]').val(id2);
        }
    })
}
function onRightClick(event, treeId, treeNode) {
    treenode11=treeNode;
    // console.log(treeNode);
    $("#fileNode_pid").val(treeNode.pid);
    $("#fileNode_pname").val(treeNode.name);
    $("#ak").val(treeNode.id);
    if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
        showRMenu("root", event.clientX, event.clientY);
    } else if (treeNode && !treeNode.noR) {
        showRMenu("node", event.clientX, event.clientY);
    }
};
//显示右键菜单
function showRMenu(type, x, y) {
    $("#rMenu").show();
    $("#rMenu").css({"top": y + "px", "left": x + "px", "visibility": "visible"}); //设置右键菜单的位置、可见
    $("body").bind("mousedown", onBodyMouseDown);
}
//隐藏右键菜单
function hideRMenu() {
    $("#rMenu").css({"visibility": "hidden"}); //设置右键菜单不可见
    $("body").unbind("mousedown", onBodyMouseDown);
}
//鼠标按下事件
function onBodyMouseDown(event) {
    if (!(event.target.id == "rMenu" || $(event.target).parents("#rMenu").length > 0)) {
        // $("#rMenu").hide();
        $("#rMenu").css({ "visibility": "hidden"});
    }
}
$("#selectadd").click(function () {
    layer.open({
        type: 2,
        title: '添加',
        content: "addcourse.html",
        area: ['80%', '80%'],
    })
})
